<?php
define('ROOT_DIR', $_SERVER['DOCUMENT_ROOT'].'/');
require(ROOT_DIR.'cofd/common.php');

$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
$sql = "SELECT exmatid, exmatname FROM exam_page WHERE exmatid LIKE ? OR exmatname LIKE ?";

$stmt = $conn->prepare($sql);
if ($stmt === false) {
    die("准备SQL语句失败: " . $mysqli->error);
}

$searchTermLike = '%' . $searchTerm . '%';
$stmt->bind_param('ss', $searchTermLike, $searchTermLike);

$stmt->execute();
$result = $stmt->get_result();

$users = array();
while ($user = $result->fetch_assoc()) {
    $users[] = $user;
}
echo json_encode(['results' => $users]);
$stmt->close();
$conn->close();
?>